package org.easydarwin.player.simpleplayer;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import org.easydarwin.video.RtspClient;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "TAG";
    private static boolean isRecording = false;

    String[] permissions = new String[]{Manifest.permission.INTERNET, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET};
    List<String> mPermissionList = new ArrayList<>();

    // private ImageView welcomeImg = null;
    private static final int PERMISSION_REQUEST = 1;
// 检查权限

    private void checkPermission() {
        mPermissionList.clear();
        //判断哪些权限未授予
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        /**
         * 判断是否为空
         */
        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
        } else {//请求权限方法
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST);
        }
    }

    /**
     * 响应授权
     * 这里不管用户是否拒绝，都进入首页，不再重复申请权限
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUEST:
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextureView textureView1 = findViewById(R.id.texture_view);
        Button record = findViewById(R.id.record);
//        TextureView textureView2 = findViewById(R.id.texture_view_2);
        checkPermission();

        RtspClient client = new RtspClient(this, "11111", textureView1, new ResultReceiver(new Handler()), new RtspClient.I420DataCallback() {
            @Override
            public void onI420Data(ByteBuffer buffer) {

            }

            @Override
            public void onPcmData(byte[] pcm) {
            }
        });
        client.play(BuildConfig.RTSP_URL);


        record.setOnClickListener(v -> {
            if (!isRecording){
                String s = new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis());
                client.startRecord("/sdcard/" + "10000000" + "_" + s + ".mp4");
                record.setText("停止录像");
                isRecording = true;
            }else {
                client.stopRecord();
                record.setText("开始录像");
                isRecording = false;
            }
        });

    }


}
